//
//  InitializationExample.swift
//  SwiftCode
//
//  Created by yuhus on 2023/12/25.
//

import Foundation

class InitializationExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "deinit", description: "子类析构器实现的最后，父类的析构器会被自动调用", action: superAutoDeinit),
    .init(title: "deinit", description: "通过最近打开项目杀死程序会触发 deinit", action: {}),
    .init(title: "deinit", description: "使用 exit(:)，不会触发 deinit", action: { exit(0) }),
  ]
}

class Vehicle {
  var numberOfWheels = 0
  var description: String {
    return "\(numberOfWheels) wheel(s)"
  }
  
  deinit {
    print("Vehicle \(description) deinit")
  }
}

class Bicycle: Vehicle {
  override init() {
    super.init()
    numberOfWheels = 2
  }
  
  deinit {
    print("Bicycle \(description) deinit")
  }
}

private func superAutoDeinit() {
  _ = Bicycle()
}
